﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ApiRiaService.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the ApiRiaService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.MVC.Services.RIA
{
    using System;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;

    using Spenta.StreetCare.Core.IoC;
    using Spenta.StreetCare.Core.Models;

    /// <summary>
    /// RIA API Service for StreetCare
    /// </summary>
    [EnableClientAccess]
    public class ApiRiaService : LinqToEntitiesDomainService<StreetCareEntities>
    {
        /// <summary>
        /// Gets the assignments.
        /// </summary>
        /// <returns>The assignments.</returns>
        public IQueryable<Assignment> GetAssignments()
        {
            return this.ObjectContext.Assignments;
        }

        /// <summary>
        /// Gets the categories.
        /// </summary>
        /// <returns>The categories.</returns>
        public IQueryable<Category> GetCategories()
        {
            return this.ObjectContext.Categories;
        }

        /// <summary>
        /// Gets the comments.
        /// </summary>
        /// <returns>The comments.</returns>
        public IQueryable<Comment> GetComments()
        {
            return this.ObjectContext.Comments;
        }

        /// <summary>
        /// Gets the customers.
        /// </summary>
        /// <returns>The customers.</returns>
        public IQueryable<Customer> GetCustomers()
        {
            return this.ObjectContext.Customers;
        }

        /// <summary>
        /// Gets the notification types.
        /// </summary>
        /// <returns>The notification types.</returns>
        public IQueryable<NotificationType> GetNotificationTypes()
        {
            return this.ObjectContext.NotificationTypes;
        }

        /// <summary>
        /// Gets the reports.
        /// </summary>
        /// <returns>the reports.</returns>
        public IQueryable<Report> GetReports()
        {
            return this.ObjectContext.Reports;
        }

        /// <summary>
        /// Gets the status.
        /// </summary>
        /// <returns>The status.</returns>
        public IQueryable<Status> GetStatus()
        {
            return this.ObjectContext.Status;
        }

        /// <summary>
        /// Gets the users.
        /// </summary>
        /// <returns>The users.</returns>
        public IQueryable<User> GetUsers()
        {
            return this.ObjectContext.Users;
        }

        /// <summary>
        /// Creates a report with the entered information.
        /// </summary>
        /// <param name="report">The report information.</param>
        /// <returns>The operation response</returns>
        [Invoke]
        public Report CreateReport(Report report)
        {
            try
            {
                // Set default status
                report.Status =
                    this.ObjectContext.Status.Where(s => s.StatusCustomerID == report.ReportCustomerID).FirstOrDefault();
                report.ReportStatusWhen = DateTime.Now;

                this.ObjectContext.Reports.AddObject(report);
                this.ObjectContext.SaveChanges();

                IoCHelper.Logger.WriteInfo("Report created. ID: " + report.ReportID + ", Title: " + report.ReportTitle);

                return report;
            }
            catch (Exception ex)
            {
                IoCHelper.Logger.WriteError("Error creating report", ex);
                throw;
            }
        }

        /// <summary>
        /// Creates a new comment with the given information
        /// </summary>
        /// <param name="comment">The comment information</param>
        /// <returns>The operation result</returns>
        [Invoke]
        public Comment CreateComment(Comment comment)
        {
            try
            {
                this.ObjectContext.Comments.AddObject(comment);
                this.ObjectContext.SaveChanges();

                IoCHelper.Logger.WriteInfo("Comment created. ID: " + comment.CommentID + ", User: " + comment.User.UserID);

                return comment;
            }
            catch (Exception ex)
            {
                IoCHelper.Logger.WriteError("Error creating comment", ex);
                throw;
            }
        }

        /// <summary>
        /// Creates the object context.
        /// </summary>
        /// <returns>The object context</returns>
        protected override StreetCareEntities CreateObjectContext()
        {
            return new StreetCareEntities(IoCHelper.ConfigurationBag.GetConnectionString("StreetCareEntities"));
        }
    }
}

